package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.Category;
import com.example.isoftservice.pojos.InstitutionBigCategory;
import com.example.isoftservice.pojos.InstitutionSmallCategory;
import com.example.isoftservice.service.CategoryService;
import com.example.isoftservice.service.InstitutionSmallCategoryService;
import com.example.isoftservice.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/smallCategory")
public class InstitutionSmallCategoryController {

    @Resource
    private InstitutionSmallCategoryService institutionSmallCategoryService;


    @RequestMapping(value = "/searchSmallCategoryList",method = RequestMethod.GET)
    public Result<Map<String,Object>> searchSmallCategoryList(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setCode(0);
        result.setResult(institutionSmallCategoryService.searchSmallCategoryList(pageNo, pageSize));
        result.setMessage("查询成功");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/searchSmallCategoryById",method = RequestMethod.GET)
    public Result<InstitutionSmallCategory> searchSmallCategoryById(Integer institutionSmallCategoryId) {
        Result<InstitutionSmallCategory> result = new Result<>();
        InstitutionSmallCategory institutionSmallCategory = institutionSmallCategoryService.searchSmallCategoryById(institutionSmallCategoryId);
        if(institutionSmallCategory != null) {
            result.setCode(0);
            result.setResult(institutionSmallCategory);
            result.setMessage("查询成功");
            return result;
        } else {
            result.setCode(1);
            result.setMessage("数据不存在");
            return result;
        }

    }


    @RequestMapping(value = "/addSmallCategory",method = RequestMethod.POST)
    public Result<Boolean> addSmallCategory(@RequestBody InstitutionSmallCategory institutionSmallCategory) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = institutionSmallCategoryService.addSmallCategory(institutionSmallCategory);
        result.setResult(flag);
        result.setMessage("增加成功");
        result.setSuccess(flag);
        return result;
    }

    @RequestMapping(value = "/removeSmallCategoryByIds",method = RequestMethod.POST)
    public Result<Boolean> removeSmallCategoryByIds(@RequestBody InstitutionSmallCategory institutionSmallCategory) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = institutionSmallCategoryService.removeSmallCategoryByIds(institutionSmallCategory.ids);
        result.setResult(flag);
        result.setMessage("删除成功");
        result.setSuccess(flag);
        return result;
    }


    @RequestMapping(value = "/modifySmallCategory",method = RequestMethod.POST)
    public Result<Boolean> modifySmallCategory(@RequestBody InstitutionSmallCategory institutionSmallCategory) {
        Result<Boolean> result = new Result<>();
        if(institutionSmallCategoryService.searchSmallCategoryById(institutionSmallCategory.getInstitutionSmallCategoryId()) == null) {
            result.setMessage("该数据不存在");
            result.setCode(1);
            return result;
        }
        result.setCode(0);
        boolean flag = institutionSmallCategoryService.modifySmallCategoryById(institutionSmallCategory);
        result.setResult(flag);
        result.setMessage("更新成功");
        result.setSuccess(flag);
        return result;
    }
}
